//
//  OrderMarketController.m
//  JF_New_DYKT
//
//  Created by 陈雷 on 2017/9/4.
//  Copyright © 2017年 陈雷. All rights reserved.
//

#import "OrderMarketController.h"
#import "OrderMarketCell.h"

@interface OrderMarketController ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,UITextFieldDelegate>

@property (nonatomic,strong) UITableView *tbv;

@property (strong, nonatomic) IBOutlet UIView *tbvHeaderV;
@property (strong, nonatomic) IBOutlet UIView *bottomV;

@property (weak, nonatomic) IBOutlet UIView *adScrollV;


@property (strong, nonatomic) IBOutlet UIView *buyOrSaleV;


@property (strong, nonatomic) IBOutlet UIView *transactionV;

@property (weak, nonatomic) IBOutlet UILabel *tranSactionTitleL;

@property (weak, nonatomic) IBOutlet UITextField *countLb;

@property (weak, nonatomic) IBOutlet UITextField *unitPriceTf;

@property (weak, nonatomic) IBOutlet UILabel *totoalPriceLb;

@property (weak, nonatomic) IBOutlet UILabel *buyOrSaleCountLb;

@property (weak, nonatomic) IBOutlet UILabel *buyOrSalePriceLb;


@property (weak, nonatomic) IBOutlet UILabel *todayPriceLb;


@property (nonatomic,copy) NSString *todayPriceStr;//今日价格

@end

@implementation OrderMarketController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.todayPriceStr = @"100";
    [self baseConfig];
    [self notificationAction];
}

- (void)baseConfig{
    
    self.title = @"订单市场";
    
    self.tbv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCreenWidth, KSCreenHeight - 113) style:UITableViewStyleGrouped];
    self.tbv.delegate = self;
    self.tbv.dataSource = self;
    [self.view addSubview:self.tbv];
    self.tbv.separatorColor = [UIColor colorWithHexString:@"eeeeee"];
    self.tbv.separatorInset = UIEdgeInsetsZero;
    self.tbv.backgroundColor = [UIColor colorWithHexString:@"f6f6f6"];
    
    self.tbv.tableFooterView = [UIView new];
    
    self.tbvHeaderV.frame = CGRectMake(0, 0, KSCreenWidth, 260);
    
    self.tbv.tableHeaderView = self.tbvHeaderV;
    
    self.bottomV.frame = CGRectMake(0, KSCreenHeight - 113, KSCreenWidth, 49);
    
    [self.view addSubview:self.bottomV];
    
    [self.tbv registerNib:[UINib nibWithNibName:@"OrderMarketCell" bundle:nil] forCellReuseIdentifier:@"OrderMarketCell"];

    
     SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KSCreenWidth - 75, self.adScrollV.Height) delegate:self placeholderImage:nil];
    cycleScrollView.scrollDirection = UICollectionViewScrollDirectionVertical;
    cycleScrollView.onlyDisplayText = YES;
    cycleScrollView.backgroundColor = [UIColor whiteColor];
    NSMutableArray *titlesArray = [NSMutableArray new];
    [titlesArray addObject:@"纯文字上下滚动轮播"];
    [titlesArray addObject:@"纯文字上下滚动轮播 -- demo轮播图4啊斯巴达克就啊上班打卡斯巴达克巴萨快点把伤口的鲍卡斯"];
    
    cycleScrollView.titlesGroup = [titlesArray copy];
    [cycleScrollView disableScrollGesture];
    [self.adScrollV addSubview:cycleScrollView];
    
    self.buyOrSaleV.frame = CGRectMake(0, 0, KSCreenWidth, KSCreenHeight);
    self.buyOrSaleV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];

    self.transactionV.frame = CGRectMake(0, 0, KSCreenWidth, KSCreenHeight);
    self.transactionV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.5];

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return .001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return .001;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    OrderMarketCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OrderMarketCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}


- (IBAction)handleAction:(UIButton *)sender {
    
    if (sender.tag == 100) {
        NSLog(@"预购");
        self.tranSactionTitleL.text = @"预购";
        self.todayPriceLb.text = [NSString stringWithFormat:@"今日价为¥%@",self.todayPriceStr];
        self.unitPriceTf.placeholder = @"预购价格需要高于今日价";
        self.buyOrSaleCountLb.text = @"预购数量";
        self.buyOrSalePriceLb.text = @"预购价格";
        
        [self.navigationController.view addSubview:self.buyOrSaleV];
        
    }else if (sender.tag == 200){
        NSLog(@"转让");
        
        [self.navigationController.view addSubview:self.transactionV];
        
    }else if (sender.tag == 300){
        NSLog(@"其他订单");
        

        
    }else if (sender.tag == 400){
        NSLog(@"我的订单");
    }
}


- (IBAction)confirmOrCancelAction:(UIButton *)sender {
    
    if (sender.tag == 10086) {
        NSLog(@"取消");
        [self.buyOrSaleV removeFromSuperview];
        [self buyOrSaleDataToZero];
        
    }else if (sender.tag == 10087){
        NSLog(@"确定");
        if ([self.unitPriceTf.text floatValue] * [self.countLb.text intValue] <= 0) {
            NSLog(@"价格为0了😯");
        }
        [self.buyOrSaleV removeFromSuperview];
        [self buyOrSaleDataToZero];
    }
    
}

#pragma amrk- 加减处理操作
- (IBAction)add_subtractAction:(UIButton *)sender {
    if (sender.tag == 10088) {
        NSLog(@"-");
        if ([self.countLb.text intValue] <= 1) {
            self.countLb.text = @"1";
            return;
        }
        self.countLb.text = [NSString stringWithFormat:@"%d",[self.countLb.text intValue] - 1];
    }else{
        NSLog(@"+");
        self.countLb.text = [NSString stringWithFormat:@"%d",[self.countLb.text intValue] + 1];
    }
    
    double totoalPrice = [self.countLb.text intValue] * [self.unitPriceTf.text floatValue];
    
    if (totoalPrice >= 99999) {
        
        [JKToast showWithText:@"价格超出可输入上限"];
        [self hiddenKeyboard];
        return;
    }
    self.totoalPriceLb.text = [NSString stringWithFormat:@"合计：%.2lf",totoalPrice];
    
}

- (void)textFieldChanged:(NSNotification *)not{
    double totoalPrice = [self.countLb.text intValue] * [self.unitPriceTf.text floatValue];
    if (totoalPrice>=99999) {
        [JKToast showWithText:@"价格超出可输入上限"];
        [self hiddenKeyboard];
        return;
    }
    self.totoalPriceLb.text = [NSString stringWithFormat:@"合计：%.2lf",totoalPrice];
    
}

#pragma mark- 数据置零处理
- (void)buyOrSaleDataToZero{

    self.countLb.text = @"1";
    self.unitPriceTf.text = @"";
    self.totoalPriceLb.text = @"合计：0";
}

- (void)hiddenKeyboard{

    [self.unitPriceTf resignFirstResponder];
    [self.countLb resignFirstResponder];
}


- (IBAction)zhuanrangStyle:(UIButton *)sender {
    
    
    self.buyOrSaleCountLb.text = @"转让数量";
    self.buyOrSalePriceLb.text = @"转让价格";
    
    if (sender.tag == 2000) {
        NSLog(@"平台");
        self.tranSactionTitleL.text = @"平台转让";
        NSString *lowPrice = [NSString stringWithFormat:@"%d",[self.todayPriceStr intValue] - 11];
        NSString *highPrice = [NSString stringWithFormat:@"%d",[self.todayPriceStr intValue] - 1];
        self.todayPriceLb.text = [NSString stringWithFormat:@"今日价为¥%@，价格区间为¥%@～¥%@",self.todayPriceStr,lowPrice,highPrice];
        self.unitPriceTf.placeholder = @"转让价格需要低于今日价";
        [self.navigationController.view addSubview:self.buyOrSaleV];
    }else if (sender.tag == 2001){
        NSLog(@"邀请码");
        
        self.tranSactionTitleL.text = @"邀请码转让";
        NSString *lowPrice = [NSString stringWithFormat:@"%d",[self.todayPriceStr intValue] - 11];
        NSString *highPrice = [NSString stringWithFormat:@"%d",[self.todayPriceStr intValue] - 1];
        self.todayPriceLb.text = [NSString stringWithFormat:@"今日价为¥%@，价格区间为¥%@～¥%@",self.todayPriceStr,lowPrice,highPrice];
        self.unitPriceTf.placeholder = @"转让价格需要低于今日价";
        [self.navigationController.view addSubview:self.buyOrSaleV];
        
    }else if (sender.tag == 2002){
        NSLog(@"好友");
    }else if (sender.tag == 2003){
        NSLog(@"取消");
    }
    [self.transactionV removeFromSuperview];
}

- (void)notificationAction{
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:nil];
    
}

- (void)keyBoardWillShow:(NSNotification *) note {
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    // 获取键盘高度
    CGRect keyBoardBounds  = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyBoardHeight = keyBoardBounds.size.height;
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.view.transform = CGAffineTransformMakeTranslation(0, -keyBoardHeight+80);
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
    
}

- (void)keyBoardWillHide:(NSNotification *) note {
    // 获取用户信息
    NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
    // 获取键盘动画时间
    CGFloat animationTime  = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    
    // 定义好动作
    void (^animation)(void) = ^void(void) {
        self.view.transform = CGAffineTransformIdentity;
    };
    
    if (animationTime > 0) {
        [UIView animateWithDuration:animationTime animations:animation];
    } else {
        animation();
    }
}

@end

